﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
//using DevExpress.LocalizationCHS;
using DevExpress.Utils;
using DevExpress.XtraEditors.Controls;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraEditors.Repository;
using JsalesModel;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Views.Base;


namespace JSales.ModuleProduct
{
    public partial class ColorSetForm : Form
    {
        public ColorSetForm()
        {
            InitializeComponent();
        }
        
        private void CtrolINI()
        {
            barButtonAdd.Enabled = true;
            barButtonSave.Enabled = false;

            gridView1.OptionsBehavior.Editable = false;
            gridView1.OptionsBehavior.AllowAddRows = DevExpress.Utils.DefaultBoolean.False;
            gridView1.OptionsView.NewItemRowPosition = NewItemRowPosition.None;
        }



        private void ColorSetForm_Load(object sender, EventArgs e)
        {
            //汉化界面
       //     DevExpress.XtraGrid.Localization.GridLocalizer.Active = new DevExpress.LocalizationCHS.DevExpressXtraGridLocalizationCHS();
       //     DevExpress.XtraTreeList.Localization.TreeListLocalizer.Active= new DevExpressXtraTreeListLocalizationCHS();
            
            gridControl1.DataSource = GlobalVar.GColors.ToList();
            //一定要转换为BindingList<>对象绑定，才能触发GridView的InitNewRow事件。

            gridView1.Columns["COLORID"].FieldName = "COLORID";
            gridView1.Columns["COLORID"].Caption = "颜色ID";
            gridView1.Columns["COLORID"].VisibleIndex = 0;
            
            gridView1.Columns["COLORNAME"].FieldName = "COLORNAME";
            gridView1.Columns["COLORNAME"].Caption = "颜色";
            gridView1.Columns["COLORNAME"].VisibleIndex = 1;

            gridView1.Columns["ISDELETED"].FieldName = "ISDELETED";
            gridView1.Columns["ISDELETED"].Caption = "停用标志";
            gridView1.Columns["ISDELETED"].VisibleIndex = 2;
            
            CtrolINI();
        }
        
        private void GridView1InitNewRow(object sender, DevExpress.XtraGrid.Views.Grid.InitNewRowEventArgs e)
        {
            DevExpress.XtraGrid.Views.Grid.GridView view = sender as DevExpress.XtraGrid.Views.Grid.GridView;
            view.SetRowCellValue(e.RowHandle, view.Columns["COLORID"], "");
            view.SetRowCellValue(e.RowHandle, view.Columns["COLORNAME"], "");
            this.gridView1.UpdateCurrentRow();
            //var user = new TbColor();
        }
        
        private void BarButtonAddItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            gridView1.OptionsBehavior.Editable = true;
            gridView1.OptionsBehavior.AllowAddRows = DevExpress.Utils.DefaultBoolean.True;
            gridView1.OptionsView.NewItemRowPosition=NewItemRowPosition.Top;
            barButtonSave.Enabled = true;
        }

        private void barButtonSave_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if(GlobalVar.GColors.Any())
            {
            //    foreach (TbColor color in GlobalVar.GColors)
            //    {
            //        switch (color.RowState)
            //        {
            //            case DataRowState.Added:
            //                TbColor color_a = new TbColor();
            //                color_a.Colorid = row["Colorid"].ToString();
            //                color_a.Colorname = row["Colorname"].ToString();
            //                color_a.Isdeleted = false;
            //                ColorClass.Create(color_a);
            //                this.ColorSetForm_Load(sender, e);
            //                break;
            //            case DataRowState.Deleted:
            //                TbColor color_d = ColorClass.Query(row["Colorid"].ToString());
            //                color_d.Isdeleted = true;
            //                ColorClass.Update(color_d);
            //                this.ColorSetForm_Load(sender, e);
            //                break;
            //            case DataRowState.Modified:
            //                TbColor color_m = ColorClass.Query(row["Colorid"].ToString());
            //                color_m.Colorid = row["Colorid"].ToString();
            //                color_m.Colorname = row["Colorname"].ToString();
            //                color_m.Isdeleted = bool.Parse(row["Isdeleted"].ToString());
            //                ColorClass.Update(color_m);
            //                this.ColorSetForm_Load(sender, e);
            //                break;
            //            case DataRowState.Unchanged:
            //                //MessageBox.Show("Unchanged");
            //                break;
            //        }
            //    } 
            //    GlobalVar.GlobaDs.Tables["Color"].AcceptChanges();
            }

        }

        private void barButtonEdit_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            gridView1.OptionsBehavior.Editable = true;
            gridView1.OptionsView.NewItemRowPosition = NewItemRowPosition.None;
            barButtonSave.Enabled = true;
        }

        private void barButtonCancel_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            gridControl1.FocusedView.CloseEditor();
            CtrolINI();
        }
        
        private void gridView1_ValidateRow(object sender, ValidateRowEventArgs e)
        {
            GridView view = sender as GridView;
            GridColumn IDColumn = view.Columns["COLORID"];
            if (view.GetRowCellValue(e.RowHandle, IDColumn).Equals("") || view.GetRowCellValue(e.RowHandle, IDColumn).Equals(null))
            {
                e.Valid = false;
                view.SetColumnError(IDColumn, "颜色ID不能为空！");
            }
            if (view.GetRowCellValue(e.RowHandle, IDColumn).ToString().Length>3)
            {
                e.Valid = false;
                view.SetColumnError(IDColumn, "颜色ID长度不能大于3！");
            }   
            



            //else
            //{
            //    if (view.GetRowCellValue(e.RowHandle, IDColumn).ToString().Length > 3)
            //    {
            //        e.Valid = false;
            //        view.SetColumnError(IDColumn, "ID长度超过3位");
            //    }
            //}
            
            
            /*
            DataRowView focusedRow = (DataRowView)gridView1.GetFocusedRow();
            if (!focusedRow.IsNew) return;
            foreach (DataColumn column in focusedRow.DataView.Table.Columns)
            {
                e.Valid = !focusedRow.Row.IsNull(column);
                if (!e.Valid) return;
            }
            */

        }

        private void gridView1_InvalidRowException(object sender, InvalidRowExceptionEventArgs e)
        {
            e.ExceptionMode = ExceptionMode.NoAction;
            //另一种异常模式
            //e.ExceptionMode = ExceptionMode.DisplayError;
            //e.WindowCaption = "输入错误！";
        }

        private void gridView1_FocusedRowChanged(object sender, FocusedRowChangedEventArgs e)
        {

        }

        private void gridControl1_Click(object sender, EventArgs e)
        {

        }
    }
}
